<?php

namespace app\admin\controller;

use app\model\MultiBizModel;
use app\model\Util;
use support\Request;
use support\Response;
use Exception;
use app\model\BizModel;

/**
 * 多商户总店管理控制器
 *
 * @package app\admin\controller
 */
class MultiController
{
    /**
     * 获取总店列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            $query = MultiBizModel::query()->orderBy('id', 'desc');
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();
            foreach ($list as &$item) {
                $item['created_at'] = $item->created_at ? date('Y-m-d H:i:s', $item->created_at) : null;
                $item['updated_at'] = $item->updated_at ? date('Y-m-d H:i:s', $item->updated_at) : null;

                $item['logo'] = Util::pathToUrl($item['logo']);

                $item['biz_total'] = BizModel::where('multi_biz_id', '=',$item['id'])->count();
            }
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 新建/编辑总店
     * @param Request $request
     * @return Response
     */
    public function submit(Request $request): Response
    {
        try {
            $data = $request->post();
            if (empty($data['name'])) {
                throw new Exception('总店名称不能为空');
            }
            if (empty($data['account'])) {
                throw new Exception('账号不能为空');
            }
            if (empty($data['id'])) {
                // 新建
                $exists = MultiBizModel::where('account', $data['account'])->count();
                if ($exists > 0) {
                    throw new Exception('账号已存在');
                }
                $multi = new MultiBizModel();
                $multi->created_at = time();
            } else {
                $multi = MultiBizModel::find($data['id']);
                if (!$multi) {
                    throw new Exception('总店不存在');
                }
            }
            $multi->name = $data['name'];
            $multi->logo = $data['logo'] ?? '';
            $multi->phone = $data['phone'] ?? '';
            $multi->account = $data['account'];
            if (!empty($data['password'])) {
                $multi->password = md5($data['password']);
            }
            $multi->updated_at = time();
            if (!$multi->save()) {
                throw new Exception(empty($data['id']) ? '添加失败' : '更新失败');
            }
            return json([
                'data' => $multi,
                'msg' => empty($data['id']) ? '添加成功' : '更新成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除总店
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = $request->post('id');
            if (empty($id)) {
                throw new Exception('参数错误');
            }
            $multi = MultiBizModel::find($id);
            if (!$multi) {
                throw new Exception('总店不存在');
            }
            if (!$multi->delete()) {
                throw new Exception('删除失败');
            }
            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    public function all(Request $request): Response
    {
        try {
            $list = MultiBizModel::select('id', 'name')->get();
            return json([
                'data' => $list,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 